package com.top.art.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.top.art.bo.ExaminationBO;
import com.top.art.bo.ExaminationPaperPageBO;
import com.top.art.constants.Status;
import com.top.art.entity.ExaminationInfo;
import com.top.art.entity.ExamineeInfo;
import com.top.art.entity.StudioInfo;
import com.top.art.enums.CourseEnum;
import com.top.art.exception.ArtException;
import com.top.art.mapper.ExamineeInfoMapper;
import com.top.art.service.IExamineeInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.top.art.service.IStudioInfoService;
import com.top.art.vo.ExaminationMessageVo;
import com.top.art.vo.ProvinceVO;
import com.top.art.vo.ScoreVO;
import com.top.art.vo.StudioVo;
import io.jsonwebtoken.lang.Assert;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.top.art.tool.Tool.notNullAndEmpty;

/**
 * <p>
 * 考生信息表 服务实现类
 * </p>
 *
 * @author hjh
 * @since 2020 -10-05
 */
@Service
public class ExamineeInfoServiceImpl extends ServiceImpl<ExamineeInfoMapper, ExamineeInfo> implements IExamineeInfoService {


    @Autowired
    private IStudioInfoService studioInfoService;

    /**
     * Page page.
     *
     * @param bo the bo
     * @return the page
     */
    @Override
    public IPage<ExamineeInfo> page(ExaminationPaperPageBO bo) {
        LambdaQueryWrapper<ExamineeInfo> queryWrapper = Wrappers.<ExamineeInfo>lambdaQuery()
                .eq(notNullAndEmpty(bo.getProvince()), ExamineeInfo::getProvince, bo.getProvince())
                .eq(ExamineeInfo::getActivityStatus, Status.VALID)
                // 画室详情
                .eq(StringUtils.isNoneBlank(bo.getStudioCode()), ExamineeInfo::getStudioCode, bo.getStudioCode())
                .eq(StringUtils.isNoneBlank(bo.getProvinceCode()), ExamineeInfo::getProvinceCode, bo.getProvinceCode())
                .eq(StringUtils.isNoneBlank(bo.getProvince()), ExamineeInfo::getProvince, bo.getProvince())
                .like(StringUtils.isNoneBlank(bo.getStudioName()), ExamineeInfo::getStudioName, bo.getStudioName())
                .orderByDesc(ExamineeInfo::getLastUpdateTime, ExamineeInfo::getId);
        if (StringUtils.isNotBlank(bo.getCourse())) {
            if (StringUtils.equals(bo.getCourse(), CourseEnum.COLOR.getName())) {
                queryWrapper.eq(ExamineeInfo::getIsColor, 1);
            } else if (StringUtils.equals(bo.getCourse(), CourseEnum.SKETCH.getName())) {
                queryWrapper.eq(ExamineeInfo::getIsSketch, 1);
            } else if (StringUtils.equals(bo.getCourse(), CourseEnum.WRITING.getName())) {
                queryWrapper.eq(ExamineeInfo::getIsWriting, 1);
            } else if (StringUtils.equals(bo.getCourse(), CourseEnum.COMPOSE.getName())) {
                queryWrapper.eq(ExamineeInfo::getIsCompose, 1);
            } else {
                throw new ArtException(5101, "科目类型没找到");
            }
        }
        return page(new Page<>(bo.getCurrent(), bo.getSize()), queryWrapper);
    }

//    @Override
//    public Map<String, List<ExamineeInfo>> groupByareaCode(ExaminationBO bo) {
//        return this.list(Wrappers.<ExamineeInfo>lambdaQuery().eq(ExamineeInfo::getProvinceCode,bo.getProviceCode()).eq(ExamineeInfo::getActivityStatus, Status.VALID))
//                .stream().collect(Collectors.groupingBy(ExamineeInfo::getStudioCode));
//    }

    @Override
    public Map<String, List<ExamineeInfo>> groupByareaCode(ExaminationBO bo) {
        return this.list(Wrappers.<ExamineeInfo>lambdaQuery().eq(ExamineeInfo::getProvinceCode, bo.getProvinceCode()).eq(ExamineeInfo::getActivityStatus, Status.VALID))
                .stream().collect(Collectors.groupingBy(ExamineeInfo::getStudioCode));
    }

    @Override
    public List<ScoreVO> queryScore() {
        return baseMapper.queryScore(null);
    }

    @Override
    public Page<ExamineeInfo> listExamineeInfo(ExaminationBO bo) {
        LambdaQueryWrapper<ExamineeInfo> queryWrapper = Wrappers.<ExamineeInfo>lambdaQuery()
                .eq(StringUtils.isNoneBlank(bo.getProviceName()), ExamineeInfo::getProvince, bo.getProviceName())
                .like(StringUtils.isNoneBlank(bo.getExamineeName()), ExamineeInfo::getExamineeName, bo.getExamineeName())
                .eq(ExamineeInfo::getActivityStatus, Status.VALID);
        return page(new Page<ExamineeInfo>(bo.getCurrent(), bo.getSize()), queryWrapper);
    }

    @Override
    public List<ProvinceVO> queryProvince() {
        return baseMapper.queryProvince();
    }

    @Override
    public StudioVo countExaminationInfo(String provinceCode) {
        Assert.notNull(provinceCode, "请先选择省份");
        Integer allPeopleCount = this.count(Wrappers.<ExamineeInfo>lambdaQuery()
                .eq(StringUtils.isNoneBlank(provinceCode), ExamineeInfo::getProvinceCode, provinceCode));

        Integer inspectionsPeopleCount = this.count(Wrappers.<ExamineeInfo>lambdaQuery()
                .eq(StringUtils.isNoneBlank(provinceCode), ExamineeInfo::getProvinceCode, provinceCode)
                .isNull(ExamineeInfo::getAdmissionTicketCode));

        StudioVo vo = new StudioVo();
        vo.setAllPeopleCount(allPeopleCount);
        vo.setInspectionsPeopleCount(inspectionsPeopleCount);
        return vo;
    }

    @Override
    public ExamineeInfo checkIdAndTicketCode(ExamineeInfo ei) {
        ExamineeInfo ret = this.getOne(Wrappers.<ExamineeInfo>lambdaQuery()
                .eq(ExamineeInfo::getAdmissionTicketCode, ei.getAdmissionTicketCode())
                .eq(ExamineeInfo::getIdentificationNumber, ei.getIdentificationNumber()));
        if (ret == null) {
            throw new ArtException(500, "身份证号或准考证号错误");
        }
        return ret;
    }
}
